package edc;

/**
 *
 * @author vpopov
 */
final public class DataConverter {

	private DataConverter() {

	}
	
	public static String asciiToHex(String asciiData) {
		StringBuilder hexDataBuilder = new StringBuilder();
		for (int i = 0; i < asciiData.length(); i++) {
			int characterCode = (int) asciiData.charAt(i);
			hexDataBuilder.append(Integer.toHexString(characterCode));
		}

		return hexDataBuilder.toString();
	}

	public static String hexToBinary(String hexData) {
		StringBuilder binaryDataBuilder = new StringBuilder();
		for (int i = 0; i < hexData.length(); i++) {
			int characterCode = Character.toUpperCase((int) hexData.charAt(i));
			int number = characterCode >= 'A' ? characterCode + 10 - 'A' : characterCode - '0';
			String binaryStringNumber = Integer.toBinaryString(number);
			while (i > 0 && binaryStringNumber.length() < 4) {
				binaryStringNumber = "0" + binaryStringNumber;
			}
			binaryDataBuilder.append(binaryStringNumber);
		}

		return binaryDataBuilder.toString();
	}
}
